package com.objectdraw.client.objectdraw;

import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.EventListener;
import javax.swing.*;
import com.objectdraw.client.*;

/**
 * Was the main class for the ObjectDraw3 project.  In collabDraw it is used 
 * to create the drawing GUI and add all of the necessary swing objects and listeners.
 * 
 * @author jgulik, cfruehan
 *
 */
@SuppressWarnings("serial")
public class ObjectDraw3 extends JApplet {

	public DrawingCanvas canvas;
	protected EventListener listener;
	protected boolean isApplet = false;
	public clientcomm com;

	/* Constructors */

	/**
	 * constructor when called as application
	 */
	public ObjectDraw3(boolean isApplet) {
		this.isApplet = isApplet;
		if (!isApplet) {
			init();
		}
	}

	/**
	 * constructor when ObjectDraw3 called as an applet
	 */
	public ObjectDraw3() {
		/* invoked as Applet */
		this(true);
	}
	
	/**
	 * constructor called w/ connection parameter
	 * 
	 * @param incom connection to be used by ObjectDraw3
	 */
	public ObjectDraw3(clientcomm incom){
		com=incom;
		init();
		canvas.control=false;
	}

	/**
	 * sets up toolbar, control panel, canvase, menu bar, and adds listeners
	 */
	public void init() {
		getContentPane().setLayout(new BorderLayout());
		canvas = createDrawingCanvas();
		canvas.com=com;
		getContentPane().add(canvas, BorderLayout.CENTER);
		listener = createDrawingCanvasListener(canvas);
		canvas.addDrawingCanvasListener(listener);
		ControlPanel controlPanel = createControlPanel();
		getContentPane().add(controlPanel, BorderLayout.SOUTH);
		EventListener controlPanelListener = createControlPanelListener(canvas);
		controlPanel.addControlPanelListener(controlPanelListener);
		ToolList toolList = createToolList();
		ToolBar toolBar = createToolBar(toolList);
		getContentPane().add(toolBar, BorderLayout.WEST);
		MenuBar menuBar = createMenuBar(toolList);
		getContentPane().add(menuBar, BorderLayout.NORTH);

		EventListener listener = createKeyboardListener(canvas);
		(canvas).addKeyboardListener(listener);
	}

	/* Factory methods */
	protected DrawingCanvas createDrawingCanvas() {
		return new DrawingCanvas();
	}

	protected EventListener createDrawingCanvasListener(DrawingCanvas c) {
		return new DrawingCanvasListener(c);
	}

	protected ControlPanel createControlPanel() {
		return new ControlPanel();
	}

	protected EventListener createControlPanelListener(DrawingCanvas canvas) {
		return new ControlPanelListener(canvas);
	}

	protected ToolBar createToolBar(ToolList toolList) {
		ToolBar toolbar = new ToolBar(toolList);
		JButton button = new JButton(new ImageIcon("images/eraser.jpg"));
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				if(canvas.magicBox.active){
					if(canvas.firstObject.next!=null){
						canvas.firstObject.next.prev=null;
						canvas.firstObject=canvas.firstObject.next;
					}else{
						canvas.firstObject=null;
						canvas.lastObject=null;
					}
					
					canvas.com.sendMessage("delete ");
					canvas.magicBox.active=false;
					repaint();
				}
			}	    	
	    });
		toolbar.add(button);
		return toolbar;
	}

	protected MenuBar createMenuBar(ToolList toolList) {
		return new MenuBar(toolList);
	}

	protected EventListener createKeyboardListener(DrawingCanvas canvas) {
		return new KeyboardListener(canvas);
	}

	/* Main method */

	/**
	 * creates frame for ObjectDraw3,  not called in collabDraw
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setTitle("ObjectDraw3");
		frame.getContentPane().setLayout(new BorderLayout());
		frame.getContentPane().add(new ObjectDraw3(false), BorderLayout.CENTER);
		frame.addWindowListener(new AppCloser());
		frame.pack();
		frame.setSize(800, 600);
		frame.setVisible(true);
	}


	protected ToolList createToolList() {
		ToolList actions = new ToolList();
		actions.add(new ToolListener("Freehand", getImageIcon("freehand.jpg"),"freehand drawing tool", canvas, new FreehandTool(canvas),com));
		actions.add(new ToolListener("Line", getImageIcon("line.jpg"),"Line drawing tool", canvas, new LineTool(canvas),com));
		actions.add(new ToolListener("Rectangle",getImageIcon("rectangle.jpg"), "Rectangle drawing tool",canvas, new RectangleTool(canvas),com));
		actions.add(new ToolListener("Oval", getImageIcon("oval.jpg"),"Oval drawing tool", canvas, new OvalTool(canvas),com));
		actions.add(new ToolListener("Text", getImageIcon("text.jpg"),"text drawing tool", canvas, new TextTool(canvas),com));
		actions.add(new ToolListener("Selection", getImageIcon("select.jpg"),"object selection tool", canvas, new SelectTool(canvas),com));
		return actions;
	}

	protected ImageIcon getImageIcon(String fileName) {
		if (isApplet) {
			try {
				URL url = new URL(getCodeBase(), fileName);
				return new ImageIcon(url);
			} catch (MalformedURLException e) {
				return null;
			}
		} else {
			return new ImageIcon("images/"+fileName);
		}
	}

	/* Inner class AppCLoser for terminating application */
	/* when Close Window button of frame is clicked */

	static class AppCloser extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	}
}
